import Vue from 'vue'
import App from './App'
import router from './router'
import VueLazyload from 'vue-lazyload'
import store from '@/store'
import '@/assets/js/remChange'
import '@/assets/js/request'
import '@/assets/css/common.scss'
import 'swiper/dist/css/swiper.min.css'

import config from '@/assets/js/config'
import filters from '@/assets/js/filters'
import tools from '@/assets/js/tools'
import { requestFullScreen } from '@/assets/js/common'

Vue.config.productionTip = false

requestFullScreen(document.body || document.documentElement)

// 初始化全局过滤器
Object.keys(filters).forEach(item => {
  Vue.filter(item, filters[item])
})

// 懒加载
Vue.use(VueLazyload, {
  attempt: 1,
  // preLoad: 1.3,
  error: require('./assets/images/404.png'),
  loading: require('./assets/images/loading-img2.gif'),
  // the default is ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend']
  listenEvents: [ 'scroll' ]
})

Vue.prototype.$tools = tools
Vue.prototype.$config = config

router.beforeEach((to, from, next) => {
  // 如果未匹配到路由
  if (to.matched.length === 0) {
    // 如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
    from.name ? next({ name: from.name }) : next('/')
  } else {
    document.querySelector('title').innerHTML = to.name
    if (to.path) _czc.push(['_trackPageview', '/#' + to.fullPath], '')
    // 如果该路由不需要登录权限
    if (to.meta.notRequireAuth) {
      next()
    } else {
      if (store.state.userInfo._id) {
        next()
      } else {
        next({
          path: '/login',
          query: {
            redirect: to.fullPath
          }
        })
      }
    }
  }
})

// 路由变化重新配置微信分享
router.afterEach(to => {
  // wxConfig(() => updateShare())
})

/* eslint-disable no-new */
const app = new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
